package com.itbour.service.yjx.trade.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.itbour.com.page.JspPage;
import com.itbour.com.page.Page;
import com.itbour.common.util.StringUtils;
import com.itbour.dao.yjx.trade.YjxFormMgrDao;
import com.itbour.dto.yjx.trade.CdInfoDto;
import com.itbour.dto.yjx.trade.YjxFormInfo;
import com.itbour.dto.yjx.trade.YjxFormMgrDto;
import com.itbour.service.yjx.trade.YjxFormMgrService;

/**
 */
public class YjxFormMgrServiceImpl implements YjxFormMgrService {
	
	private YjxFormMgrDao yjxFormMgrDao;

	public Page getFormMgrInfo(Map<String, Object> parm, Integer pageNo,
			Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = yjxFormMgrDao.getFormMgrCnt(parm);
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			parm.put("pageSize", pageSize);
			parm.put("skipResults", skipResults);
			List<YjxFormMgrDto> data = yjxFormMgrDao.getFormMgrInfo(parm);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}
	
	// 获取 用户表单反馈列表
	public Page pageGetFormInfoMgr(Map<String, Object> parm, Integer pageNo,
			Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = yjxFormMgrDao.getFormInfoMgrCnt(parm);
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			parm.put("pageSize", pageSize);
			parm.put("skipResults", skipResults);
			List<YjxFormInfo> data = yjxFormMgrDao.getFormInfoMgr(parm);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}

	public boolean editFormMgrInfo(YjxFormMgrDto yjxPriceInfoDto) {
		Map<String, Object> parm =new HashMap<String, Object>();
		try {
			if (yjxPriceInfoDto.getForm_type()==null){
				return false;
			}
			parm.put("form_type",yjxPriceInfoDto.getForm_type());
			parm.put("key1",yjxPriceInfoDto.getKey1());
			parm.put("key2",yjxPriceInfoDto.getKey2());
			parm.put("key3",yjxPriceInfoDto.getKey3());
			parm.put("key4",yjxPriceInfoDto.getKey4());
			parm.put("key5",yjxPriceInfoDto.getKey5());
			parm.put("key6",yjxPriceInfoDto.getKey6());
			parm.put("key7",yjxPriceInfoDto.getKey7());
			parm.put("key8",yjxPriceInfoDto.getKey8());
			parm.put("key9",yjxPriceInfoDto.getKey9());
			parm.put("key10",yjxPriceInfoDto.getKey10());
			parm.put("thumb",yjxPriceInfoDto.getThumb());
			parm.put("desc",yjxPriceInfoDto.getDesc());
			if (yjxPriceInfoDto.getForm_id()==null){
				parm.put("update_by","add:"+yjxPriceInfoDto.getUpdate_by());
				yjxFormMgrDao.inserFormMgrInfo(parm);
			} else {
				parm.put("update_by","upd:"+yjxPriceInfoDto.getUpdate_by());
				parm.put("form_id",yjxPriceInfoDto.getForm_id());
				yjxFormMgrDao.updFormMgrInfo(parm);
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public boolean delFormMgr(Map<String, Object> parm) {
		try {
			yjxFormMgrDao.delFormMgrInfo(parm);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public YjxFormMgrDao getYjxFormMgrDao() {
		return yjxFormMgrDao;
	}

	public void setYjxFormMgrDao(YjxFormMgrDao yjxFormMgrDao) {
		this.yjxFormMgrDao = yjxFormMgrDao;
	}

	public List<YjxFormMgrDto> getOneFormMgr(Map<String, Object> parm) {
		return yjxFormMgrDao.getFormMgrInfo(parm);
	}

	public List<YjxFormInfo> getOneFormInfo(Map<String, Object> parm) {
		return yjxFormMgrDao.getFormInfoMgr(parm);
	}
	
	
	// 小程序文案列表
	public Page pageGetCdInfo(Map<String, Object> parm, Integer pageNo,
			Integer pageSize) {
		try {
			Page result = null;
			int skipResults = JspPage.getStartOfPage(pageNo, pageSize);
			int totalSize = yjxFormMgrDao.getCdInfoCnt(parm);
			while(totalSize >0 && totalSize <= skipResults){
				skipResults= skipResults - pageSize;
				pageNo = pageNo -1;
			}
			parm.put("pageSize", pageSize);
			parm.put("skipResults", skipResults);
			List<CdInfoDto> data = yjxFormMgrDao.pageGetCdInfo(parm);
			result = new JspPage(skipResults, totalSize, pageSize, data);
			return result;
		} finally {
		}
	}

	@Override
	public void addCdInfo(CdInfoDto cdInfoDto) throws Exception {
		Map<String, Object> parm = new HashMap<String, Object>();

		if (cdInfoDto.getInfo_id()!=null && 
				Integer.parseInt(cdInfoDto.getInfo_id().toString())>0){
			
			parm.put("info_id", cdInfoDto.getInfo_id());
			List<CdInfoDto> list = yjxFormMgrDao.getOneNoDelCdInfo(parm);
			if (list.size()>0){
				throw new Exception("项目ID已存在");
			}
		}
		parm.put("type",cdInfoDto.getType());
		parm.put("type_name",cdInfoDto.getType_name());
		parm.put("state",0);
		parm.put("value",cdInfoDto.getValue());
		parm.put("update_by", cdInfoDto.getUpdate_by());
		yjxFormMgrDao.addCdInfo(parm);
	}

	@Override
	public void updateCdInfo(CdInfoDto cdInfoDto) {
		// TODO Auto-generated method stub
		Map<String, Object> parm = new HashMap<String, Object>();
		parm.put("info_id",cdInfoDto.getInfo_id());
		if (cdInfoDto.getInfo_id() == 0){
			new Exception("参数为空 不能修改");
		}
		
		if (cdInfoDto.getType()!=null){
			parm.put("type",cdInfoDto.getType());
		}
		if (StringUtils.isNotEmpty(cdInfoDto.getType_name())){
			parm.put("type_name",cdInfoDto.getType_name());
		}
		if (StringUtils.isNotEmpty(cdInfoDto.getValue())){
			parm.put("value",cdInfoDto.getValue());
		}
		if (cdInfoDto.getState()!=null){
			parm.put("state",cdInfoDto.getState());
		}
		parm.put("update_by", cdInfoDto.getUpdate_by());
		yjxFormMgrDao.updateCdInfo(parm);
	}

	@Override
	public void delCdInfo(Map<String, Object> parm) {
		// TODO Auto-generated method stub
		yjxFormMgrDao.delCdInfo(parm);
	}

	@Override
	public List<CdInfoDto> getOneCdInfo(Map<String, Object> parm) {
		// TODO Auto-generated method stub
		return yjxFormMgrDao.pageGetCdInfo(parm);
	}
	
}
